package com.zhu.ttwords.view.fragment;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhu.ttwords.R;
import com.zhu.ttwords.TTWORDS;
import com.zhu.ttwords.presenter.StudyPresenter;
import com.zhu.ttwords.view.activity.IStudyActivity;
import com.zhu.ttwords.view.adapter.StudyAdapter;
import com.zhu.ttwords.view.listener.StudyListener;

import java.util.List;
import java.util.Locale;

/**
 * Created by KnightYe on 2015/4/12.
 */
public class StudyFragment extends Fragment implements IStudyActivity {
    Activity activity;
    StudyPresenter presenter;
    ImageView mao;
    ViewPager viewPager;
    StudyAdapter adapter;
    StudyListener listener;
    TextView index_total;
    TextView showMode;

    Dialog examComplateDialog;
    Dialog studyComplateDialog;
    int exam_total = 1;
    int exam_right = 0;
    String exam_percent = "0.00%";

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_study, container, false);
        index_total = (TextView) root.findViewById(R.id.fragment_study_index_total);
        showMode = (TextView) root.findViewById(R.id.fragment_study_showmode);
        mao = (ImageView) root.findViewById(R.id.fragment_study_mao);
        viewPager = (ViewPager) root.findViewById(R.id.fragment_study_viewpager);

        return root;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        presenter = new StudyPresenter(this);
        presenter.initStudy();

        listener = new StudyListener(presenter);
        viewPager.setOnPageChangeListener(listener);
        viewPager.setOffscreenPageLimit(TTWORDS.getAppSetting().getGroup());
        studyComplateDialog = new AlertDialog.Builder(activity)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("进行测试才能保留学习成果哦，亲！")
                .setItems(new String[]{"● 开始测试", "● 结束学习", "● 再看一会"},
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                switch (which) {
                                    case 0:
                                        presenter.initExam();
                                        break;
                                    case 1:
                                        activity.finish();
                                        break;
                                    case 2:
                                        dialog.dismiss();
                                        break;
                                }
                            }
                        }).create();


        mao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.showMenu();
            }
        });
        selectInputMethod();
    }

    @Override
    public void initExamData(List<Object> mData) {
        adapter = new StudyAdapter(activity, mData, MODE_EXAM, listener);
        viewPager.setAdapter(adapter);
        showMode.setText("测试模式");
        index_total.setText("1/" + adapter.getCount());
    }

    @Override
    public void initStudyData(List<Object> mData, boolean isReview) {
        adapter = new StudyAdapter(activity, mData, MODE_STUDY, listener);
        viewPager.setAdapter(adapter);
        index_total.setText("1/" + adapter.getCount());
        if (isReview) {
            showMode.setText("复习模式");
        } else {
            showMode.setText("学习模式");
        }
    }


    @Override
    public void updatePageIndex(int index) {
        index_total.setText(index + "/" + adapter.getCount());
    }

    @Override
    public void updateExamData(int right) {
        exam_right = right;
        exam_total = adapter.getCount();
        exam_percent = String.format("%.2f", (exam_right
                * 1.0 / exam_total * 100.0)) + "%";
        examComplateDialog = new AlertDialog.Builder(activity)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("没复习完就不能学习新词哦，么么哒！")
                .setItems(
                        new String[]{
                                "正确数： " + exam_right +
                                        "\n错误数： " + (exam_total - exam_right)
                                        + "\n正确率： " + exam_percent,
                                "● 新的测试",
                                "● 学习模式",
                                "● 结束测试"},
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(
                                    DialogInterface dialog,
                                    int which) {
                                switch (which) {
                                    case 0:
                                        examComplateDialog.dismiss();
                                        break;
                                    case 1:
                                        presenter.initExam();
                                        break;
                                    case 2:
                                        presenter.initStudy();
                                        break;
                                    case 3:
                                        activity.finish();
                                        break;
                                }
                            }
                        }).create();
    }

    @Override
    public void updateStudyData(int right) {
        if (right == adapter.getCount()) {
            studyComplateDialog.show();
        }
    }

    @Override
    public void showMenu(int mode) {
        switch (mode) {
            case MODE_STUDY:
                studyComplateDialog.show();
                break;
            case MODE_EXAM:
                examComplateDialog.show();
                break;
        }
    }


    private void selectInputMethod() {
        InputMethodManager immanager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        List<InputMethodInfo> list = immanager.getEnabledInputMethodList();
        String defaultIme = Settings.Secure.getString(activity.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
        InputMethodInfo default_info = null;
        boolean hasJPInpute = false;
        for (InputMethodInfo inputMethodInfo : list) {
            if (inputMethodInfo.getId().equals(defaultIme)) {
                default_info = inputMethodInfo;
            }

            if (!(inputMethodInfo.getSubtypeCount() > 0 && inputMethodInfo
                    .getSubtypeAt(0).getLocale()
                    .equals(Locale.JAPAN.toString()))) {
                hasJPInpute = true;
            }
        }
        if (default_info != null && hasJPInpute) {
            immanager.showInputMethodPicker();
        } else {
            Toast.makeText(activity, "请安装日语输入法", Toast.LENGTH_SHORT).show();
        }
    }
}
